/*
 * Copyright 2010 Assert Developments
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.ad.coffeebreak.services;

import be.ad.coffeebreak.domain.ProjectHours;
import org.apache.log4j.Logger;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;

/**
 * A bean implementation of project hours services.
 *
 * @author Stefan Bangels
 */
@Stateless
@SuppressWarnings("unused")
public class ProjectHoursServiceBean implements ProjectHoursService {

    private final Logger auditLogger = Logger.getLogger("audit");
    private final Logger logger = Logger.getLogger(getClass());

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @TransactionAttribute(TransactionAttributeType.SUPPORTS)
    public ProjectHours findProjectHours(long projectId, Date date) {
        try {
            TypedQuery<ProjectHours> query = entityManager.createNamedQuery(
                    ProjectHours.QUERY_FIND_PROJECT_HOURS, ProjectHours.class
            );
            query.setParameter(ProjectHours.QUERY_PARAM_PROJECT_ID, projectId);
            query.setParameter(ProjectHours.QUERY_PARAM_DATE, date);
            return query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    @Override
    public void saveProjectHours(ProjectHours projectHours) {
        ProjectHours existing = findProjectHours(projectHours.getProjectId(), projectHours.getDate());
        if (existing != null) {
            projectHours.setId(existing.getId());
            auditLogger.info("updating project hours: " + projectHours);
            entityManager.merge(projectHours);
        } else {
            auditLogger.info("inserting project hours: " + projectHours);
            entityManager.persist(projectHours);
        }
    }

    @Override
    public void deleteProjectHours(long projectId, Date date) {
        auditLogger.info("deleting project hours with projectId=" + projectId + " and date=" + date);
        ProjectHours projectHours = findProjectHours(projectId, date);
        if (projectHours == null) {
            logger.warn("cannot delete project hours, project not found! " +
                    "(projectId=" + projectId + ", date=" + date + ")");
            return;
        }
        entityManager.remove(projectHours);
    }

}
